<?php
/**
 * Created by PhpStorm.
 * User: mac
 * Date: 16/8/20
 * Time: 上午10:57
 */

namespace app\admin\controller;


use app\admin\model\ArticleCate;
use think\Controller;
use think\Loader;
use think\Request;
use think\Session;
use think\View;

class Article extends Controller{

    public function __construct(Request $request)
    {
        parent::__construct($request);
        if (!Session::get("login")){
            $this->error("请登录","User/login");
        }
    }

    /**
     * @return View
     * 文章列表
     */
    public function article_list(){
        $article = new \app\admin\model\Article();
        $this->assign('articles',$article::all());
        return view();
    }

    public function article_oper(){
        if (Request::instance()->param('oper')==2 && Request::instance()->param('id')){
            $article = new \app\admin\model\Article();
            $rs = $article::destroy(Request::instance()->param('id'));
        }
        if ($rs){
            $this->success("操作成功");
        }
    }

    /**
     * @return View
     * 文章增加
     */
    public function addarticle(){


        if (Request::instance()->post()){
            $article = new \app\admin\model\Article();
            //图片处理
            if (Request::instance()->file()){
                $file = Request::instance()->file('image');
                $path = ROOT_PATH."public".DS."uploads".DS."images";
                $info = $file->move($path);
                if($info){
//                    print_r($info);
                    $filename = DS."uploads".DS."images".DS.date("Ymd").DS.$info->getFilename();
                    $article->image = $filename;
                }else{
                }
            }
            $article->title = Request::instance()->post('title');
            $article->cate = Request::instance()->post('cate_id');
            $article->keywords = Request::instance()->post('keywords');
            $article->description = Request::instance()->post('description');
            $article->content = Request::instance()->post('content');
            if (Request::instance()->post('ishot')){
                $article->ishot = 1;
            }else{
                $article->ishot = 0;
            }

            if ($article->save()){
                $this->success("添加成功","Article/article_list");
            }else{
                $this->error("添加失败");
            }
        }else{
            $articleCates = new ArticleCate();
            $articles = $articleCates::all(['status'=>1]);
            $this->assign('cates',$articles);
            return view();
        }
    }

    public function article_info(){
        $article = new \app\admin\model\Article();
        if (Request::instance()->param('id')){
            $info = $article::get(Request::instance()->param('id'));
        }
        if (!$info){
            $this->error("没有内容");
        }
        $this->assign("info",$info);
        return view();
    }

    public function article_category(){
        $article = new ArticleCate();
        $articles = $article::all(['status'=>1]);
        $this->assign('cates',$articles);
        return view();
    }

    public function article_cate_oper(){
        $article = new ArticleCate();
        if (Request::instance()->param('oper')==1 && Request::instance()->param('id')){
            //禁用
            $rs = $article->save(['status'=>0],['id'=>Request::instance()->param('id')]);
        }elseif (Request::instance()->param('oper')==2 && Request::instance()->param('id')){
            //删除
            $rs = $article::destroy(Request::instance()->param('id'));
        }
        if ($rs){
            $this->success("操作成功");
        }
    }

    public function addcate(){
        if (Request::instance()->post()){
            $article = new ArticleCate();
            $article->cate_name = Request::instance()->post("cate_name");
            $article->keywords = Request::instance()->post("keywords");
            $article->description = Request::instance()->post("description");
            $article->sort = Request::instance()->post("sort");
            if ($article->save()>0){
                $this->success("添加成功",'Article/article_category');
            }else{
                $this->error("添加失败");
            }
        }else{
            return view();
        }

    }
}